<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once "class/Load.class.php";
require_once "class/Config.class.php";
require_once "class/Inputs.class.php";
require_once "class/Tooltip.class.php";
require_once "class/StatusRoster.class.php";
require_once "class/SessionRoster.class.php";
require_once "class/Localization.class.php";
require_once "class/SubscriptionManager.class.php";

$load = Load::GetInstance();
$config = Config::GetInstance();
$inputs = Inputs::GetInstance();
$tooltip = Tooltip::GetInstance();
$localization = Localization::GetInstance();
$status_roster = StatusRoster::GetInstance();
$session_roster = SessionRoster::GetInstance();

if ( $session_roster->IsConnected() )
{
	$session_roster->Redirect("send.php");
}

if ( $inputs->HasPostData('register') )
{
	$subscription_manager = SubscriptionManager::GetInstance();
	
	if ( $inputs->Post('name') == null or $inputs->Post('nickname') == null )
	{
		$status_roster->AddError( $localization->Get('you must enter a nickname and a login') );
	}
	elseif ( $subscription_manager->CheckNameAndNickname($inputs->Post('name'), $inputs->Post('nickname')) == false )
	{
		$status_roster->AddError( $localization->Get('your login and your nickname must be different') );
	}
	else
	{
		switch ( $subscription_manager->HasNicknameOrName($inputs->Post('nickname'), $inputs->Post('name')) )
		{
			case SUBSCRIPTION_MANAGER_NICKNAME_EXISTS:
				$status_roster->AddError( $localization->Get('this nickname is already taken') );
				break;
			case SUBSCRIPTION_MANAGER_LOGIN_EXISTS:
				$status_roster->AddError( $localization->Get('this login is already taken') );
				break;
		}
	}
	
	if ( $status_roster->HasError() == false )
	{
		if ( $inputs->Post('rosterpass') != $config->GetRosterPassword() )
		{
			$status_roster->AddError( $localization->Get('the roster s password is not correct') );
		}
		elseif ( $subscription_manager->CheckPassword($inputs->Post('password')) == false )
		{
			$status_roster->AddError( $localization->Get('your password is too short (%s characters min)', $subscription_manager->GetPasswordMinimumSize()) );
		}
		elseif ( $subscription_manager->Register($inputs->Post('nickname'), $inputs->Post('name'), $inputs->Post('password'), $config->GetLocalization()) == false)
		{
			$status_roster->AddError( $localization->Get('an error occured during the registering of the account') );
		}
		else
		{
			$status_roster->AddSuccess( $localization->Get('account registered successfully. you can now %s login', "connection.php") );
		}
	}
	
	$session_roster->SaveStatus($status_roster);
	$session_roster->Redirect("register.php");
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
	<head>
		<title>LZRoster<?php echo $config->HasRosterTitle() ?  " - ".$config->GetRosterTitle() : null; ?></title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<link rel="stylesheet" media="screen" type="text/css" href="styles/_.css" />
		<link rel="stylesheet" media="screen" type="text/css" href="styles/register.css" />
		<script type="text/javascript" src="js/overlib.js"><!-- overLIB (c) Erik Bosrup --></script>
	</head>
	<body>
		
		<?php require_once "inc/head.php"; ?>
		<?php require_once "inc/hmenu.php"; ?>
		
		<h2><?php echo $localization->Get('register'); ?></h2>
		
		<?php
		if ($status_roster->HasMessage() or $session_roster->HasStatus())
		{
		?>
			
			<p>
				<?php echo $localization->Get('this page produced some messages. please refer to the following lines'); ?>
			</p>
			
			<?php echo $session_roster->HasStatus() ? $session_roster->GetStatus()->GetStack_InList() : $status_roster->GetStack_InList(); ?>
			
			<p>
				<?php echo $localization->Get('you can refresh the page %s and try again', "register.php"); ?>
			</p>
			
		<?php
		}
		else
		{
		?>
		
		<form action="register.php" method="post" id="formconnect" class="send">
		<p id="introduction">
			<em><?php echo $localization->Get('please choose your login information'); ?></em>
		</p>
		
		<p id="nickname">
			<label for="nickname">
				<?php echo $localization->Get('nickname'); ?>:
			</label>
			<input
				type="text"
				name="nickname"
				maxlength="15"
				id="nickname"
				onmouseover="return overlib('<?php echo $tooltip->Flatten( $localization->Get('your nickname is the name under the one you will (..)') ); ?>', CAPTION, '<?php echo $tooltip->Flatten($localization->Get('nickname')); ?>', WIDTH, 200);"
				onmouseout="return nd();"
			/>
		</p>
		
		<p id="loginpassword">
			<label for="name">
				<?php echo $localization->Get('login'); ?>:
			</label>
			<input
				type="text"
				name="name"
				maxlength="15"
				id="name"
				onmouseover="return overlib('<?php echo $tooltip->Flatten( $localization->Get('the login is case insensitive') ); ?>', CAPTION, '<?php echo $tooltip->Flatten($localization->Get('login')); ?>', WIDTH, 200);"
				onmouseout="return nd();"
			/><br />
			
			<label for="password">
				<?php echo $localization->Get('password'); ?>:
			</label>
			<input
				type="password"
				name="password"
				maxlength="15"
				id="password"
			/>
		</p>
		
		<p id="rosterintroduction">
			<em><?php echo $localization->Get('please enter the rosters password'); ?></em>
		</p>
		
		<p id="rosterspassword">
			<label for="rosterpass">
				<?php echo $localization->Get('roster'); ?>:
			</label>
			<input
				type="password"
				name="rosterpass"
				id="rosterpass"
			/>
		</p>
		
		<p id="submit">
			<input type="submit" name="register" value="<?php echo $localization->Get('register'); ?>" />
		</p>
		</form>
		
		<?php
		}
		?>
		
		<?php require_once "inc/foot.php"; ?>
		
	</body>
</html>